package org.dce.screencapturer.objects;

import java.awt.Rectangle;

@SuppressWarnings("serial")
public abstract class RegularShapeObject 
	extends DotBasedShapeObject{

	protected final Dot top = new Dot(false,true,false,true);
	protected final Dot bottom = new Dot(false,true,false,true);
	protected final Dot left = new Dot(true,false,true,false);
	protected final Dot right = new Dot(true,false,true,false);
	
	private final Dot[] dots = {top,bottom,left,right};
	
	protected RegularShapeObject(){
		initDots(getDots());
	}
	
	@Override
	protected Dot[] getDots() {
		return dots;
	}

	@Override
	protected void onInsertionProgress() {
		
		Rectangle r = getBounds(getInsertionFirstPoint(), getInsertionSecondPoint());
		
		setLocation(r.getLocation());
		
		right.setLocation(r.width-right.getWidth(), r.height/2);
		left.setLocation(0,r.height/2);
		top.setLocation(r.width/2,0);
		bottom.setLocation(r.width/2,r.height-40);
		
	}

	@Override
	protected void onInsertionDone() {
		
		Rectangle r = getBounds(getInsertionFirstPoint(), getInsertionSecondPoint());

		setLocation(r.getLocation());

		right.setLocation(r.width-right.getWidth(), r.height/2);
		left.setLocation(0,r.height/2);
		top.setLocation(r.width/2,0);
		bottom.setLocation(r.width/2,r.height-40);
		
	}
	
	protected Rectangle getDotsBounds(){
		return new Rectangle(
			left.getCenterX(),
			top.getCenterY(),
			right.getCenterX()-left.getCenterX(),
			bottom.getCenterY()-top.getCenterY());
	}
	
	@Override
	protected void notifyDotMoved(Dot dot) {
		
		if (dot == right || dot == left){
			
			int centerX = left.getCenter().x + (right.getCenter().x - left.getCenter().x)/2;
			
			bottom.setLocation(centerX,bottom.getY());
			top.setLocation(centerX,top.getY());
			
		} else if (dot == top || dot == bottom){
			
			int centerY = top.getCenter().y + (bottom.getCenter().y - top.getCenter().y)/2;
			
			left.setLocation(left.getX(),centerY);
			right.setLocation(right.getX(),centerY);
		}
		
		super.notifyDotMoved(dot);
	}

}
